package com.example.module_home.testmvi2

import android.os.Bundle
import android.util.Log
import android.view.View

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.lib_mvicore.ui.BaseMVIActivity
import com.example.module_home.R
import com.example.module_home.databinding.ActivityMvi2Binding


import kotlinx.coroutines.launch

class Mvi2Activity :BaseMVIActivity(){
//    private val viewmodel:VideoTypeViewModel by viewModels()
    private lateinit var viewmodel: VideTypeViewModel

    val binding:ActivityMvi2Binding by lazy { ActivityMvi2Binding.inflate(layoutInflater) }

    override fun generateViewModel() {
        viewmodel = ViewModelProvider(this)[VideTypeViewModel::class.java]
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            //start活跃状态订阅流信息
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewmodel.intents.send(VideoTypeIntent.GetSimpleType)
            }
        }
    }
    /**
     * 处理状态
     */
    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewmodel.getSimpleTypeState.collect{
                    when(it){
                        is VideoTypeState.GetSimpleTypeSuccess->{
                            Log.d("mvi", "数据加载成功: ${it.data}")
                        }
                        is VideoTypeState.GetMutilTypeSuccess->{

                        }
                        is VideoTypeState.GetMutilTypeByPidSuccess -> {

                        }
                        is VideoTypeState.Loading->{
                            Log.d("mvi", "数据加载中: ")

                        }
                        is VideoTypeState.Error->{
                            Log.d("mvi", "数据加载失败: ${it.msg}")
                        }
                        is VideoTypeState.Empty->{
                            Log.d("mvi", "空数据:")
                        }

                    }
                }
            }
        }
    }
}